package com.anolesoft.yr.mbus.crm.mapper;

import com.anolesoft.yr.mbus.crm.entity.CrmBasePartPriceDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/8/12 9:20
 */
@Mapper
public interface CrmBasePartPriceMapper {
    @Insert("insert into crm_base_part_price (" +
            "objuid," +
            "fac_code," +
            "cont_main_uid," +
            "pre_order_uid," +
            "prod_product_uid," +
            "crm_base_uid," +
            "part_model_uid," +
            "offer_person," +
            "offer_time," +
            "offer_price," +
//            "deal_price," +
            "c_user_uid," +
            "c_time," +
            "m_user_uid," +
            "m_time," +
            "del_flag," +
            "remark" +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{contMainUid,jdbcType=VARCHAR}," +
            "#{preOrderUid,jdbcType=VARCHAR}," +
            "#{prodProductUid,jdbcType=VARCHAR}," +
            "#{crmBaseUid,jdbcType=VARCHAR}," +
            "#{partModelUid,jdbcType=VARCHAR}," +
            "#{offerPerson,jdbcType=VARCHAR}," +
            "#{offerTime,jdbcType=TIMESTAMP}," +
            "#{offerPrice,jdbcType=DECIMAL}," +
//            "#{dealPrice,jdbcType=DECIMAL}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "0," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(CrmBasePartPriceDO crmBasePartPriceDO);

    @Update("update crm_base_part_price set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "cont_main_uid = #{contMainUid,jdbcType=VARCHAR}," +
            "pre_order_uid = #{preOrderUid,jdbcType=VARCHAR}," +
            "prod_product_uid = #{prodProductUid,jdbcType=VARCHAR}," +
            "crm_base_uid = #{crmBaseUid,jdbcType=VARCHAR}," +
            "part_model_uid = #{partModelUid,jdbcType=VARCHAR}," +
            "offer_person = #{offerPerson,jdbcType=VARCHAR}," +
            "offer_time = #{offerTime,jdbcType=TIMESTAMP}," +
            "offer_price = #{offerPrice,jdbcType=DECIMAL}," +
//            "deal_price = #{dealPrice,jdbcType=DECIMAL}," +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}," +
            "m_time = now()," +
            "remark = #{remark,jdbcType=VARCHAR}" +
            " where objuid = #{objuid,jdbcType=VARCHAR}")
    int update(CrmBasePartPriceDO crmBasePartPriceDO);

    @Update("update crm_base_part_price set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "crmBasePartPriceResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "cont_main_uid", property = "contMainUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pre_order_uid", property = "preOrderUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "prod_product_uid", property = "prodProductUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "crm_base_uid", property = "crmBaseUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "part_model_uid", property = "partModelUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "offer_person", property = "offerPerson", jdbcType = JdbcType.VARCHAR),
            @Result(column = "offer_time", property = "offerTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "offer_price", property = "offerPrice", jdbcType = JdbcType.DECIMAL),
//            @Result(column = "deal_price", property = "dealPrice", jdbcType = JdbcType.DECIMAL),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "crm_base_uid", property = "crmBase", one = @One(select = "com.anolesoft.yr.mbus.crm.mapper.CrmBaseMapper.selectOneById" , fetchType = FetchType.LAZY)),
            @Result(column = "part_model_uid", property = "partModel", one = @One(select = "com.anolesoft.yr.mbus.part.mapper.PartModelMapper.selectOneById" , fetchType = FetchType.LAZY)),
            @Result(column = "pre_order_uid", property = "preOrder", one = @One(select = "com.anolesoft.yr.mbus.pre.mapper.PreOrderMapper.selectOneById" , fetchType = FetchType.LAZY)),
    })
    @Select("select " +
            "objuid, fac_code, " +
            "cont_main_uid, pre_order_uid, prod_product_uid, crm_base_uid, part_model_uid, offer_person, offer_time, offer_price, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from crm_base_part_price " +
            "where del_flag = 0")
    List<CrmBasePartPriceDO> selectAll();

    @ResultMap("crmBasePartPriceResultMap")
    @Select("select " +
            "objuid, fac_code, " +
            "cont_main_uid, pre_order_uid, prod_product_uid, crm_base_uid, part_model_uid, offer_person, offer_time, offer_price, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from crm_base_part_price " +
            "where del_flag = 0")
    List<CrmBasePartPriceDO> selectPageAll(Page<CrmBasePartPriceDO> page);

    @ResultMap("crmBasePartPriceResultMap")
    @Select("select " +
            "objuid, fac_code, " +
            "cont_main_uid, pre_order_uid, prod_product_uid, crm_base_uid, part_model_uid, offer_person, offer_time, offer_price, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from crm_base_part_price " +
            "where del_flag = 0 and objuid = #{id,jdbcType=VARCHAR}")
    CrmBasePartPriceDO selectOneById(@Param("id") String id);

    @Select("<script>" +
            "select " +
            "objuid, fac_code, " +
            "cont_main_uid, pre_order_uid, prod_product_uid, crm_base_uid, part_model_uid, offer_person, offer_time, offer_price, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from crm_base_part_price " +
            "where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.crmBaseUid !=null and o.crmBaseUid != '' \">" +
            "   and crm_base_uid = #{o.crmBaseUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partModelUid !=null and o.partModelUid != '' \">" +
            "   and part_model_uid = #{o.partModelUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.offerPerson !=null and o.offerPerson != '' \">" +
            "   and offer_person = #{o.offerPerson, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    @ResultMap("crmBasePartPriceResultMap")
    List<CrmBasePartPriceDO> selectList(@Param("o") CrmBasePartPriceDO crmBasePartPriceDO);

    @Select("<script>" +
            "select " +
            "objuid, fac_code, " +
            "cont_main_uid, pre_order_uid, prod_product_uid, crm_base_uid, part_model_uid, offer_person, offer_time, offer_price, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from crm_base_part_price " +
            "where del_flag = 0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.crmBaseUid !=null and o.crmBaseUid != '' \">" +
            "   and crm_base_uid = #{o.crmBaseUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.partModelUid !=null and o.partModelUid != '' \">" +
            "   and part_model_uid = #{o.partModelUid, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.offerPerson !=null and o.offerPerson != '' \">" +
            "   and offer_person = #{o.offerPerson, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    @ResultMap("crmBasePartPriceResultMap")
    List<CrmBasePartPriceDO> selectPageList(@Param("o") CrmBasePartPriceDO crmBasePartPriceDO, Page<CrmBasePartPriceDO> page);

    List<CrmBasePartPriceDO> selectPageConditionList(@Param("o") CrmBasePartPriceDO crmBasePartPriceDO, Page<CrmBasePartPriceDO> page);
}
